package com.course.${module}.controller.admin;

import com.course.server.dto.${Domain}Dto;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.service.${Domain}Service;
import com.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 〈一句话功能简述>
 * 〈〉
 *
 * @Author metype
 * @Create 2021/5/25
 * @Since 1.0.0
 */
@RestController
@RequestMapping("/admin/${domain}")
public class ${Domain}Controller {

    private static final Logger LOG = LoggerFactory.getLogger(${Domain}Controller.class);
    public static final String BUSINESS_NAME = "${tableNameCn}";

    @Autowired
    private ${Domain}Service ${domain}Service;

    @RequestMapping("/list")
    public ResponseDto<PageDto> test(@RequestBody PageDto pageDto) {
        LOG.info("pageDto:{}", pageDto);
        ${domain}Service.list(pageDto);
        return new ResponseDto <>(pageDto);
    }

    @RequestMapping("/save")
    public ResponseDto<${Domain}Dto> save(@RequestBody ${Domain}Dto ${domain}Dto) {
        LOG.info("${domain}Dto:{}", ${domain}Dto);
        // 保存校验
        <#list fieldList as field>
            <#if !field.nullAble>
        ValidatorUtil.require(${domain}Dto.get${field.nameBigHump}(), "${field.nameCn}");
            </#if>
            <#if (field.length > 0 )>
        ValidatorUtil.length(${domain}Dto.get${field.nameBigHump}(), "${field.nameCn}", 1 , ${field.length?c});
            </#if>
        </#list>

        ${domain}Service.save(${domain}Dto);
        return new ResponseDto <>(${domain}Dto);
    }

    @RequestMapping("/del/{id}")
    public ResponseDto del(@PathVariable String id) {
        LOG.info("id:{}", id);
        ${domain}Service.del(id);
        return new ResponseDto<>("200","删除成功！");
    }

}